//index.js
//获取应用实例
const app = getApp()
const action = require('../../utils/action.js');
Page({
  data: {
    currentTab : 0,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    currentSlider : 0,
    timelines : [],
    currentTimeline:0,
    scrollLeft:0,
    products:[]
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onReady: function(){
    this.loadTimeline();
    this.loadProducts();
    action.test();
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    var that = this;

    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }});  

    setInterval(function(){
      that.setData({
        currentSlider: that.data.currentSlider + 1,
      });

      if(that.data.currentSlider>=3)
      {
        that.setData({
          currentSlider: 0,
        });
      }
      //console.log(that.data.currentSlider);
    },3000);
  },
  /** 
     * 滑动切换tab 
     */
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  }, 
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    console.log(e);
    this.setData({
      currentTab: e.currentTarget.dataset.id
    });
    var goTo = '/pages/quick/index?timeline=' + this.data.currentTab;
    getApp().globalData.timelineTab = this.data.currentTab;
    console.log(getApp().globalData.timelineTab);
    wx.switchTab({
      url: goTo
    })
    console.log(goTo);
  },
  loadTimeline: function () {
    var self = this;
    app.httpGet('/index.php?r=v1/timeline/index',{
      success: function (res) {
        console.log(res);
        self.setData({ 
          timelines: res.data.items,
          currentTimeline: res.data.current
          });
        self.scrollTab();
        console.log(self.data.timelines);
      }
    });
  },
  scrollTab: function(){
    var data = this.data;
    console.log("currentTimeline:"+data.currentTimeline);
    if(data.currentTab==0)
    {
      var total = data.timelines.length;
      if(data.currentTimeline>0)
      {
        if (data.currentTimeline > (total - 5))
        {
          var offset = (total - 5) * 75;
          this.setData({ scrollLeft: offset, currentTab: data.timelines[data.currentTimeline].id });
        }
        else
        {
          var offset = (data.currentTimeline) * 75;
          console.log("offset:"+offset);
          this.setData({ scrollLeft: offset, currentTab: data.timelines[data.currentTimeline].id});
        }
      }
      console.log("currentTab:" + data.timelines[data.currentTimeline].id);
    }
  },
  loadProducts: function(){
    var self = this;
    app.httpGet('/index.php?r=v1/home/index', {
      success:function(res){
        console.log(res);
        self.setData({products:res.data});
      }
    });
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  payDeposit: function(e){
    wx.navigateTo({
      url: '/pages/my/paydeposit',
    })
  },
  doPayment: function(){
    wx.navigateTo({
      url: '/pages/my/paydeposit',
    })
  },
  bindMobile: function (e) {
    var self = this;
    console.log(e.detail.errMsg);
    console.log(e.detail.iv);
    console.log(e.detail.encryptedData);

    app.httpPut('/index.php?r=v1/phone/index', {
      success: function (res) {
        if (res.code == 200) {
          app.success("绑定成功");
          self.setData({ profile: res.data });
        }
        else {
          app.toast("绑定失败,请稍后重试");
        }
      }
    }, {
        iv: e.detail.iv,
        encryptedData: e.detail.encryptedData
      });
  },
  onItemClicked: function(e){
    console.log(e);
    wx.navigateTo({
      url: '/pages/product/view?id=' + e.currentTarget.dataset.id,
    })
  }
})
